/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Latinobarometer

--------------------------------------------------------------------------------*/

*Latinobarometer 1

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_1995_data_english_v2014_06_27.dta"

decode pais, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 1

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p27m==1|p27m==2
replace trust_govt = 0 if p27m==3|p27m==4

gen trust_justice = 1 if p27d==1|p27d==2
replace trust_justice = 0 if p27d==3|p27d==4

gen trust_police = 1 if p27h==1|p27h==2
replace trust_police = 0 if p27h==3|p27h==4

gen trust_civil_service = 1 if p27g==1|p27g==2
replace trust_civil_service = 0 if p27g==3|p27g==4

gen trust_local_govt = .

gen trust_parties = 1 if p27j==1|p27j==2
replace trust_parties = 0 if p27j==3|p27j==4

gen trust_parliament = 1 if p27i==1|p27i==2
replace trust_parliament = 0 if p27i==3|p27i==4

gen trust_others = .

gen trust_military = 1 if p27b==1|p27b==2
replace trust_military = 0 if p27b==3|p27b==4

gen trust_religious_organizations = 1 if p27a==1|p27a==2
replace trust_religious_organizations = 0 if p27a==3|p27a==4

pca trust_govt trust_justice trust_police trust_civil_service trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numero

clonevar age = s2
replace age = . if age==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(p9, 13, 14, 15)
replace religion = 3 if inlist(p9, 9)
replace religion = 4 if inlist(p9, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(p9, 11, 12, 16)
replace religion = -99 if inlist(p9, -1, -2)
replace religion = -99 if p9==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if s20==2|s20==3
replace edu = 2 if s20==4|s20==5
replace edu = 3 if s20==6|s20==7
replace edu = 4 if s20==1
replace edu = -99 if s20==-3|s20==-2|s20==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s11, 6, 7)
replace occupation = 2 if inlist(s11, 1, 5, 2)
replace occupation = 6 if inlist(s11, 3)
replace occupation = 9 if inlist(s11, 4)
replace occupation = -99 if inlist(s11, -2, -3, 8)
replace occupation = -99 if s11==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s10, 1, 2, 3)
replace employment_status = 2 if inlist(s10, 4, 5, 6, 7)
replace employment_status = -99 if s10==-2|s10==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*			3   Other
*		  -99   Missing

gen marital = 1 if s6==1
replace marital = 2 if s6==2|s6==3
replace marital = -99 if s6==.|s6==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino1_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 2

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_1996_datos_english_v2014_06_27.dta"

decode pais, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 2

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p33m==1|p33m==2
replace trust_govt = 0 if p33m==3|p33m==4

gen trust_justice = 1 if p33d==1|p33d==2
replace trust_justice = 0 if p33d==3|p33d==4

gen trust_police = 1 if p33h==1|p33h==2
replace trust_police = 0 if p33h==3|p33h==4

gen trust_civil_service = 1 if p33g==1|p33g==2
replace trust_civil_service = 0 if p33g==3|p33g==4

gen trust_local_govt = .

gen trust_parties = 1 if p33j==1|p33j==2
replace trust_parties = 0 if p33j==3|p33j==4

gen trust_parliament = 1 if p33i==1|p33i==2
replace trust_parliament = 0 if p33i==3|p33i==4

gen trust_others = 1 if p12==1
replace trust_others = 0 if p12==2

gen trust_military = 1 if p33b==1|p33b==2
replace trust_military = 0 if p33b==3|p33b==4

gen trust_religious_organizations = 1 if p33a==1|p33a==2
replace trust_religious_organizations = 0 if p33a==3|p33a==4

pca trust_govt trust_justice trust_police trust_civil_service trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numero

clonevar age = s2
replace age = . if age==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing  

gen religion = .
replace religion = 1 if inlist(p7, 13, 14, 15)
replace religion = 3 if inlist(p7, 9)
replace religion = 4 if inlist(p7, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(p7, 11, 12, 16, 17)
replace religion = -99 if inlist(p7, -1, -2)
replace religion = -99 if p7==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if s16a==2|s16a==3
replace edu = 2 if s16a==4|s16a==5
replace edu = 3 if s16a==6|s16a==7
replace edu = 4 if s16a==1
replace edu = -99 if s16a==-5|s16a==-2|s16a==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s8, 6, 7)
replace occupation = 2 if inlist(s8, 1, 5, 2)
replace occupation = 6 if inlist(s8, 3)
replace occupation = 9 if inlist(s8, 4)
replace occupation = -99 if inlist(s8, -2, -3, 8)
replace occupation = -99 if s8==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s18a, 1, 2, 3)
replace employment_status = 2 if inlist(s18a, 4, 5, 6, 7)
replace employment_status = -99 if s18a==-2|s18a==-3|s18a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s5==1
replace marital = 2 if s5==2|s5==3
replace marital = -99 if s5==.|s5==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino2_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 3

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_1997_datos_english_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 3

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if sp63c==1|sp63c==2
replace trust_justice = 0 if sp63c==3|sp63c==4

gen trust_police = 1 if sp63e==1|sp63e==2
replace trust_police = 0 if sp63e==3|sp63e==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if sp63g==1|sp63g==2
replace trust_parties = 0 if sp63g==3|sp63g==4

gen trust_parliament = 1 if sp63f==1|sp63f==2
replace trust_parliament = 0 if sp63f==3|sp63f==4

gen trust_others = 1 if sp21==1
replace trust_others = 0 if sp21==2

gen trust_military = 1 if sp63b==1|sp63b==2
replace trust_military = 0 if sp63b==3|sp63b==4

gen trust_religious_organizations = 1 if sp63a==1|sp63a==2
replace trust_religious_organizations = 0 if sp63a==3|sp63a==4

pca trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if age==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(sp85, 13, 14, 15)
replace religion = 3 if inlist(sp85, 9)
replace religion = 4 if inlist(sp85, 1, 2, 3, 4, 5, 6, 7, 8, 10, 18)
replace religion = 6 if inlist(sp85, 11, 12, 16, 17, 19)
replace religion = -99 if inlist(sp85, -1, -2)
replace religion = -99 if sp85==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if s12a==2|s12a==3
replace edu = 2 if s12a==4|s12a==5
replace edu = 3 if s12a==6|s12a==7
replace edu = 4 if s12a==1
replace edu = -99 if s12a==-2|s12a==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s8, 6, 7)
replace occupation = 2 if inlist(s8, 1, 5, 2)
replace occupation = 6 if inlist(s8, 3)
replace occupation = 9 if inlist(s8, 4)
replace occupation = -99 if inlist(s8, -2, -3, 8)
replace occupation = -99 if s8==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s7a, 1, 2, 3)
replace employment_status = 2 if inlist(s7a, 4, 5, 6, 7)
replace employment_status = -99 if s7a==-2|s7a==-3|s7a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s5==1
replace marital = 2 if s5==2|s5==3
replace marital = -99 if s5==.|s5==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino3_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 4

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_1998_datos_english_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 4

gen weight = pondera

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if sp38c==1|sp38c==2
replace trust_justice = 0 if sp38c==3|sp38c==4

gen trust_police = 1 if sp38e==1|sp38e==2
replace trust_police = 0 if sp38e==3|sp38e==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if sp38g==1|sp38g==2
replace trust_parties = 0 if sp38g==3|sp38g==4

gen trust_parliament = 1 if sp38f==1|sp38f==2
replace trust_parliament = 0 if sp38f==3|sp38f==4

gen trust_others = 1 if sp20==1
replace trust_others = 0 if sp20==2

gen trust_military = 1 if sp38b==1|sp38b==2
replace trust_military = 0 if sp38b==3|sp38b==4

gen trust_religious_organizations = 1 if sp38a==1|sp38a==2
replace trust_religious_organizations = 0 if sp38a==3|sp38a==4

pca trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if age==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(sp80, 13, 14, 15)
replace religion = 3 if inlist(sp80, 9)
replace religion = 4 if inlist(sp80, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(sp80, 11, 12, 16)
replace religion = -99 if inlist(sp80, -1, -2)
replace religion = -99 if sp80==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if s14a==2|s14a==3
replace edu = 2 if s14a==4|s14a==5
replace edu = 3 if s14a==6|s14a==7
replace edu = 4 if s14a==1
replace edu = -99 if s14a==-2|s14a==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s8, 6, 7)
replace occupation = 2 if inlist(s8, 1, 5, 2)
replace occupation = 6 if inlist(s8, 3)
replace occupation = 9 if inlist(s8, 4)
replace occupation = -99 if inlist(s8, -2, -3, 8)
replace occupation = -99 if s8==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s7a, 1, 2, 3)
replace employment_status = 2 if inlist(s7a, 4, 5, 6, 7)
replace employment_status = -99 if s7a==-2|s7a==-3|s7a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s5==1
replace marital = 2 if s5==2|s5==3
replace marital = -99 if s5==.|s5==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight

sort ccode wave survey_year

save "$data_dir/clean/latino4_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 5

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2000_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 5

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if P35ST_C==1|P35ST_C==2
replace trust_justice = 0 if P35ST_C==3|P35ST_C==4

gen trust_police = 1 if P35ST_E==1|P35ST_E==2
replace trust_police = 0 if P35ST_E==3|P35ST_E==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if P35ST_G==1|P35ST_G==2
replace trust_parties = 0 if P35ST_G==3|P35ST_G==4

gen trust_parliament = 1 if P35ST_F==1|P35ST_F==2
replace trust_parliament = 0 if P35ST_F==3|P35ST_F==4

gen trust_others = 1 if P17ST==1
replace trust_others = 0 if P17ST==2

gen trust_military = 1 if P35ST_B==1|P35ST_B==2
replace trust_military = 0 if P35ST_B==3|P35ST_B==4

gen trust_religious_organizations = 1 if P35ST_A==1|P35ST_A==2
replace trust_religious_organizations = 0 if P35ST_A==3|P35ST_A==4

pca trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = S2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if S1==1
replace sex = 2 if S1==2
replace sex = -99 if S1==.|S1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(P76ST, 13, 14, 15)
replace religion = 3 if inlist(P76ST, 9)
replace religion = 4 if inlist(P76ST, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(P76ST, 11, 12, 16)
replace religion = -99 if inlist(P76ST, -1, -2)
replace religion = -99 if P76ST==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S9, 6, 7)
replace occupation = 2 if inlist(S9, 1, 5, 2)
replace occupation = 6 if inlist(S9, 3)
replace occupation = 9 if inlist(S9, 4)
replace occupation = -99 if inlist(S9, -2, -3, 8)
replace occupation = -99 if S9==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S8A, 1, 2, 3)
replace employment_status = 2 if inlist(S8A, 4, 5, 6, 7)
replace employment_status = -99 if S8A==-2|S8A==-3|S8A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S4==1
replace marital = 2 if S4==2|S4==3
replace marital = -99 if S4==.|S4==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino5_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 6

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2001_datos_english_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 6

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p43nas==1|p43nas==2
replace trust_govt = 0 if p43nas==3|p43nas==4

gen trust_justice = 1 if p61stc==1|p61stc==2
replace trust_justice = 0 if p61stc==3|p61stc==4

gen trust_police = 1 if p61ste==1|p61ste==2
replace trust_police = 0 if p61ste==3|p61ste==4

gen trust_civil_service = 1 if p63stb==1|p63stb==2
replace trust_civil_service = 0 if p63stb==3|p63stb==4

gen trust_local_govt = 1 if p63stc==1|p63stc==2
replace trust_local_govt = 0 if p63stc==3|p63stc==4

gen trust_parties = 1 if p61stg==1|p61stg==2
replace trust_parties = 0 if p61stg==3|p61stg==4

gen trust_parliament = 1 if p61stf==1|p61stf==2
replace trust_parliament = 0 if p61stf==3|p61stf==4

gen trust_others = 1 if p42st==1
replace trust_others = 0 if p42st==2

gen trust_military = 1 if p61stb==1|p61stb==2
replace trust_military = 0 if p61stb==3|p61stb==4

gen trust_religious_organizations = 1 if p61sta==1|p61sta==2
replace trust_religious_organizations = 0 if p61sta==3|p61sta==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if s2==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(p86st, 13, 14, 15)
replace religion = 3 if inlist(p86st, 9)
replace religion = 4 if inlist(p86st, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(p86st, 11, 12, 16, 17, 18)
replace religion = -99 if inlist(p86st, -1, -2)
replace religion = -99 if p86st==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s9, 6, 7)
replace occupation = 2 if inlist(s9, 1, 5, 2)
replace occupation = 6 if inlist(s9, 3)
replace occupation = 9 if inlist(s9, 4)
replace occupation = -99 if inlist(s9, -2, -3, 8)
replace occupation = -99 if s9==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s8a, 1, 2, 3)
replace employment_status = 2 if inlist(s8a, 4, 5, 6, 7)
replace employment_status = -99 if s8a==-2|s8a==-3|s8a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s4==1
replace marital = 2 if s4==2|s4==3
replace marital = -99 if s4==.|s4==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino6_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 7

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2002_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 7

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p34std==1|p34std==2
replace trust_govt = 0 if p34std==3|p34std==4

gen trust_justice = 1 if p34stc==1|p34stc==2
replace trust_justice = 0 if p34stc==3|p34stc==4

gen trust_police = 1 if p36ste==1|p36ste==2
replace trust_police = 0 if p36ste==3|p36ste==4

gen trust_civil_service = .

gen trust_local_govt = 1 if p36stc==1|p36stc==2
replace trust_local_govt = 0 if p36stc==3|p36stc==4

gen trust_parties = 1 if p34stf==1|p34stf==2
replace trust_parties = 0 if p34stf==3|p34stf==4

gen trust_parliament = 1 if p36std==1|p36std==2
replace trust_parliament = 0 if p36std==3|p36std==4

gen trust_others = 1 if p29st==1
replace trust_others = 0 if p29st==2

gen trust_military = 1 if p36stb==1|p36stb==2
replace trust_military = 0 if p36stb==3|p36stb==4

gen trust_religious_organizations = 1 if p34sta==1|p34sta==2
replace trust_religious_organizations = 0 if p34sta==3|p34sta==4

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if s2==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(p65st, 13, 14, 15)
replace religion = 3 if inlist(p65st, 9)
replace religion = 4 if inlist(p65st, 1, 2, 3, 4, 5, 6, 7, 8, 10, 18)
replace religion = 5 if inlist(p65st, 17)
replace religion = 6 if inlist(p65st, 11, 12, 16)
replace religion = -99 if inlist(p65st, -1, -2)
replace religion = -99 if p65st==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s9, 6, 7)
replace occupation = 2 if inlist(s9, 1, 5, 2)
replace occupation = 6 if inlist(s9, 3)
replace occupation = 9 if inlist(s9, 4)
replace occupation = -99 if inlist(s9, -2, -3, 8)
replace occupation = -99 if s9==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s8a, 1, 2, 3)
replace employment_status = 2 if inlist(s8a, 4, 5, 6, 7)
replace employment_status = -99 if s8a==-2|s8a==-3|s8a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s4==1
replace marital = 2 if s4==2|s4==3
replace marital = -99 if s4==.|s4==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino7_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 8

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2003_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 8

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p23stg==1|p23stg==2
replace trust_govt = 0 if p23stg==3|p23stg==4

gen trust_justice = 1 if p21ste==1|p21ste==2
replace trust_justice = 0 if p21ste==3|p21ste==4

gen trust_police = 1 if p21stb==1|p21stb==2
replace trust_police = 0 if p21stb==3|p21stb==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if p21std==1|p21std==2
replace trust_parties = 0 if p21std==3|p21std==4

gen trust_parliament = 1 if p21stf==1|p21stf==2
replace trust_parliament = 0 if p21stf==3|p21stf==4

gen trust_others = 1 if p20st==1
replace trust_others = 0 if p20st==2

gen trust_military = 1 if p21stg==1|p21stg==2
replace trust_military = 0 if p21stg==3|p21stg==4

gen trust_religious_organizations = 1 if p21sta==1|p21sta==2
replace trust_religious_organizations = 0 if p21sta==3|p21sta==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if s2==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(p91st, 13, 14, 97)
replace religion = 3 if inlist(p91st, 9)
replace religion = 4 if inlist(p91st, 1, 2, 3, 4, 5, 6, 7, 8, 10, 16)
replace religion = 5 if inlist(p91st, 15)
replace religion = 6 if inlist(p91st, 11, 12)
replace religion = -99 if inlist(p91st, -1, -2)
replace religion = -99 if p91st==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if s18==2|s18==3
replace edu = 2 if s18==4|s18==5
replace edu = 3 if s18==6|s18==7
replace edu = 4 if s18==1
replace edu = -99 if s18==-2|s18==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s9, 6, 7)
replace occupation = 2 if inlist(s9, 1, 5, 2)
replace occupation = 6 if inlist(s9, 3)
replace occupation = 9 if inlist(s9, 4)
replace occupation = -99 if inlist(s9, -2, -3, 8)
replace occupation = -99 if s9==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s8a, 1, 2, 3)
replace employment_status = 2 if inlist(s8a, 4, 5, 6, 7)
replace employment_status = -99 if s8a==-2|s8a==-3|s8a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s4==1
replace marital = 2 if s4==2|s4==3
replace marital = -99 if s4==.|s4==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino8_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 9

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2004_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 9

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p32std==1|p32std==2
replace trust_govt = 0 if p32std==3|p32std==4

gen trust_justice = 1 if p34stb==1|p34stb==2
replace trust_justice = 0 if p34stb==3|p34stb==4

gen trust_police = 1 if p32stb==1|p32stb==2
replace trust_police = 0 if p32stb==3|p32stb==4

gen trust_civil_service = .

gen trust_local_govt = 1 if p34sth==1|p34sth==2
replace trust_local_govt = 0 if p34sth==3|p34sth==4

gen trust_parties = 1 if p34std==1|p34std==2
replace trust_parties = 0 if p34std==3|p34std==4

gen trust_parliament = 1 if p34stf==1|p34stf==2
replace trust_parliament = 0 if p34stf==3|p34stf==4

gen trust_others = 1 if p43st==1
replace trust_others = 0 if p43st==2

gen trust_military = 1 if p32stg==1|p32stg==2
replace trust_military = 0 if p32stg==3|p32stg==4

gen trust_religious_organizations = 1 if p32stc==1|p32stc==2
replace trust_religious_organizations = 0 if p32stc==3|p32stc==4

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s2
replace age = . if s2==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s1==1
replace sex = 2 if s1==2
replace sex = -99 if s1==.|s1==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(p90st, 13, 14, 97)
replace religion = 3 if inlist(p90st, 9)
replace religion = 4 if inlist(p90st, 1, 2, 3, 4, 5, 6, 7, 8, 10, 16)
replace religion = 5 if inlist(p90st, 15)
replace religion = 6 if inlist(p90st, 11, 12, 96)
replace religion = -99 if inlist(p90st, -1, -2)
replace religion = -99 if p90st==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s9, 6, 7)
replace occupation = 2 if inlist(s9, 1, 5, 2)
replace occupation = 6 if inlist(s9, 3)
replace occupation = 9 if inlist(s9, 4)
replace occupation = -99 if inlist(s9, -2, -3, 8)
replace occupation = -99 if s9==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s8a, 1, 2, 3)
replace employment_status = 2 if inlist(s8a, 4, 5, 6, 7)
replace employment_status = -99 if s8a==-2|s8a==-3|s8a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s4==1
replace marital = 2 if s4==2|s4==3
replace marital = -99 if s4==.|s4==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino9_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 10

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2005_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="República Dominicana"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 10

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p45stc==1|p45stc==2
replace trust_govt = 0 if p45stc==3|p45stc==4

gen trust_justice = 1 if p42std==1|p42std==2
replace trust_justice = 0 if p42std==3|p42std==4

gen trust_police = 1 if p42stf==1|p42stf==2
replace trust_police = 0 if p42stf==3|p42stf==4

gen trust_civil_service = .

gen trust_local_govt = 1 if p42ste==1|p42ste==2
replace trust_local_govt = 0 if p42ste==3|p42ste==4

gen trust_parties = 1 if p47stb==1|p47stb==2
replace trust_parties = 0 if p47stb==3|p47stb==4

gen trust_parliament = 1 if p45sta==1|p45sta==2
replace trust_parliament = 0 if p45sta==3|p45sta==4

gen trust_others = 1 if p14st==1
replace trust_others = 0 if p14st==2

gen trust_military = 1 if p42stb==1|p42stb==2
replace trust_military = 0 if p42stb==3|p42stb==4

gen trust_religious_organizations = 1 if p42sta==1|p42sta==2
replace trust_religious_organizations = 0 if p42sta==3|p42sta==4

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s7
replace age = . if s7==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s6==1
replace sex = 2 if s6==2
replace sex = -99 if s6==.|s6==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s2, 13, 14, 97)
replace religion = 3 if inlist(s2, 9)
replace religion = 4 if inlist(s2, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s2, 11, 12, 96)
replace religion = -99 if inlist(s2, -1, -2)
replace religion = -99 if s2==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s14, 6, 7)
replace occupation = 2 if inlist(s14, 1, 5, 2)
replace occupation = 6 if inlist(s14, 3)
replace occupation = 9 if inlist(s14, 4)
replace occupation = -99 if inlist(s14, -2, -3, 8)
replace occupation = -99 if s14==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s13a, 1, 2, 3)
replace employment_status = 2 if inlist(s13a, 4, 5, 6, 7)
replace employment_status = -99 if s13a==-2|s13a==-3|s13a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s9==1
replace marital = 2 if s9==2|s9==3
replace marital = -99 if s9==.|s9==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino10_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 11

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2006_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 11

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p32st_a==1|p32st_a==2
replace trust_govt = 0 if p32st_a==3|p32st_a==4

gen trust_justice = 1 if p24st_a==1|p24st_a==2
replace trust_justice = 0 if p24st_a==3|p24st_a==4

gen trust_police = 1 if p24st_d==1|p24st_d==2
replace trust_police = 0 if p24st_d==3|p24st_d==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if p24st_c==1|p24st_c==2
replace trust_parties = 0 if p24st_c==3|p24st_c==4

gen trust_parliament = 1 if p24st_f==1|p24st_f==2
replace trust_parliament = 0 if p24st_f==3|p24st_f==4

gen trust_others = 1 if p45st==1
replace trust_others = 0 if p45st==2

gen trust_military = 1 if p32st_e==1|p32st_e==2
replace trust_military = 0 if p32st_e==3|p32st_e==4

gen trust_religious_organizations = 1 if p32st_d==1|p32st_d==2
replace trust_religious_organizations = 0 if p32st_d==3|p32st_d==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s7
replace age = . if s7==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s6==1
replace sex = 2 if s6==2
replace sex = -99 if s6==.|s6==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s2, 13, 14, 97)
replace religion = 3 if inlist(s2, 9)
replace religion = 4 if inlist(s2, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s2, 11, 12, 96)
replace religion = -99 if inlist(s2, -1, -2)
replace religion = -99 if s2==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s14, 6, 7)
replace occupation = 2 if inlist(s14, 1, 5, 2)
replace occupation = 6 if inlist(s14, 3)
replace occupation = 9 if inlist(s14, 4)
replace occupation = -99 if inlist(s14, -2, -3, 8)
replace occupation = -99 if s14==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s13a, 1, 2, 3)
replace employment_status = 2 if inlist(s13a, 4, 5, 6, 7)
replace employment_status = -99 if s13a==-2|s13a==-3|s13a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s9==1
replace marital = 2 if s9==2|s9==3
replace marital = -99 if s9==.|s9==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino11_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 12

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2007_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 12

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p24st_a==1|p24st_a==2
replace trust_govt = 0 if p24st_a==3|p24st_a==4

gen trust_justice = 1 if p24st_d==1|p24st_d==2
replace trust_justice = 0 if p24st_d==3|p24st_d==4

gen trust_police = 1 if p27st_f==1|p27st_f==2
replace trust_police = 0 if p27st_f==3|p27st_f==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if p27st_e==1|p27st_e==2
replace trust_parties = 0 if p27st_e==3|p27st_e==4

gen trust_parliament = 1 if p24st_f==1|p24st_f==2
replace trust_parliament = 0 if p24st_f==3|p24st_f==4

gen trust_others = 1 if p23st==1
replace trust_others = 0 if p23st==2

gen trust_military = 1 if p27st_d==1|p27st_d==2
replace trust_military = 0 if p27st_d==3|p27st_d==4

gen trust_religious_organizations = 1 if p27st_c==1|p27st_c==2
replace trust_religious_organizations = 0 if p27st_c==3|p27st_c==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s11
replace age = . if s11==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s10==1
replace sex = 2 if s10==2
replace sex = -99 if s10==.|s10==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s4, 13, 14, 97)
replace religion = 3 if inlist(s4, 9)
replace religion = 4 if inlist(s4, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s4, 11, 12, 96)
replace religion = -99 if inlist(s4, -1, -2)
replace religion = -99 if s4==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s18, 6, 7)
replace occupation = 2 if inlist(s18, 1, 5, 2)
replace occupation = 6 if inlist(s18, 3)
replace occupation = 9 if inlist(s18, 4)
replace occupation = -99 if inlist(s18, -2, -3, 8)
replace occupation = -99 if s18==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s17a, 1, 2, 3)
replace employment_status = 2 if inlist(s17a, 4, 5, 6, 7)
replace employment_status = -99 if s17a==-2|s17a==-3|s17a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s13==1
replace marital = 2 if s13==2|s13==3
replace marital = -99 if s13==.|s13==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino12_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 13

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2008_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 13

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p31s_ta==1|p31s_ta==2
replace trust_govt = 0 if p31s_ta==3|p31s_ta==4

gen trust_justice = 1 if p28st_b==1|p28st_b==2
replace trust_justice = 0 if p28st_b==3|p28st_b==4

gen trust_police = 1 if p31st_c==1|p31st_c==2
replace trust_police = 0 if p31st_c==3|p31st_c==4

gen trust_civil_service = 1 if p28st_e==1|p28st_e==2
replace trust_civil_service = 0 if p28st_e==3|p28st_e==4

gen trust_local_govt = 1 if p28st_f==1|p28st_f==2
replace trust_local_govt = 0 if p28st_f==3|p28st_f==4

gen trust_parties = 1 if p28st_c==1|p28st_c==2
replace trust_parties = 0 if p28st_c==3|p28st_c==4

gen trust_parliament = 1 if p28st_a==1|p28st_a==2
replace trust_parliament = 0 if p28st_a==3|p28st_a==4

gen trust_others = 1 if p21wvsst==1
replace trust_others = 0 if p21wvsst==2

gen trust_military = 1 if p28st_d==1|p28st_d==2
replace trust_military = 0 if p28st_d==3|p28st_d==4

gen trust_religious_organizations = 1 if p28st_g==1|p28st_g==2
replace trust_religious_organizations = 0 if p28st_g==3|p28st_g==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s9
replace age = . if s9==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s8==1
replace sex = 2 if s8==2
replace sex = -99 if s8==.|s8==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s5, 13, 14, 97)
replace religion = 3 if inlist(s5, 9)
replace religion = 4 if inlist(s5, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s5, 11, 12, 96)
replace religion = -99 if inlist(s5, -1, -2)
replace religion = -99 if s5==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0|reeduc1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s18, 6, 7)
replace occupation = 2 if inlist(s18, 1, 5, 2)
replace occupation = 6 if inlist(s18, 3)
replace occupation = 9 if inlist(s18, 4)
replace occupation = -99 if inlist(s18, -2, -3, 8)
replace occupation = -99 if s18==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s17a, 1, 2, 3)
replace employment_status = 2 if inlist(s17a, 4, 5, 6, 7)
replace employment_status = -99 if s17a==-2|s17a==-3|s17a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s3==1
replace marital = 2 if s3==2|s3==3
replace marital = -99 if s3==.|s3==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino13_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 14

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2009_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 14

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p24st_a==1|p24st_a==2
replace trust_govt = 0 if p24st_a==3|p24st_a==4

gen trust_justice = 1 if p26st_b==1|p26st_b==2
replace trust_justice = 0 if p26st_b==3|p26st_b==4

gen trust_police = 1 if p24st_c==1|p24st_c==2
replace trust_police = 0 if p24st_c==3|p24st_c==4

gen trust_civil_service = 1 if p26st_e==1|p26st_e==2
replace trust_civil_service = 0 if p26st_e==3|p26st_e==4

gen trust_local_govt = 1 if p26st_f==1|p26st_f==2
replace trust_local_govt = 0 if p26st_f==3|p26st_f==4

gen trust_parties = 1 if p26st_c==1|p26st_c==2
replace trust_parties = 0 if p26st_c==3|p26st_c==4

gen trust_parliament = 1 if p26st_a==1|p26st_a==2
replace trust_parliament = 0 if p26st_a==3|p26st_a==4

gen trust_others = 1 if p58st==1
replace trust_others = 0 if p58st==2

gen trust_military = 1 if p26st_d==1|p26st_d==2
replace trust_military = 0 if p26st_d==3|p26st_d==4

gen trust_religious_organizations = 1 if p26st_g==1|p26st_g==2
replace trust_religious_organizations = 0 if p26st_g==3|p26st_g==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = s6
replace age = . if s6==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if s5==1
replace sex = 2 if s5==2
replace sex = -99 if s5==.|s5==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s7, 13, 14, 97)
replace religion = 3 if inlist(s7, 9)
replace religion = 4 if inlist(s7, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s7, 11, 12, 96)
replace religion = -99 if inlist(s7, -1, -2)
replace religion = -99 if s7==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc1==2|reeduc1==3
replace edu = 2 if reeduc1==4|reeduc1==5
replace edu = 3 if reeduc1==6|reeduc1==7
replace edu = 4 if reeduc1==1
replace edu = -99 if reeduc1==-2|reeduc1==.|reeduc1==0|reeduc1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s15, 6, 7)
replace occupation = 2 if inlist(s15, 1, 5, 2)
replace occupation = 6 if inlist(s15, 3)
replace occupation = 9 if inlist(s15, 4)
replace occupation = -99 if inlist(s15, -2, -3, 8)
replace occupation = -99 if s15==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s14a, 1, 2, 3)
replace employment_status = 2 if inlist(s14a, 4, 5, 6, 7)
replace employment_status = -99 if s14a==-2|s14a==-3|s14a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if s3==1
replace marital = 2 if s3==2|s3==3
replace marital = -99 if s3==.|s3==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino14_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 15

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2010_datos_eng_v2014_06_27.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 15

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P18ST_A==1|P18ST_A==2
replace trust_govt = 0 if P18ST_A==3|P18ST_A==4

gen trust_justice = 1 if P20ST_B==1|P20ST_B==2
replace trust_justice = 0 if P20ST_B==3|P20ST_B==4

gen trust_police = 1 if P18ST_C==1|P18ST_C==2
replace trust_police = 0 if P18ST_C==3|P18ST_C==4

gen trust_civil_service = 1 if P20ST_E==1|P20ST_E==2
replace trust_civil_service = 0 if P20ST_E==3|P20ST_E==4

gen trust_local_govt = 1 if P20ST_F==1|P20ST_F==2
replace trust_local_govt = 0 if P20ST_F==3|P20ST_F==4

gen trust_parties = 1 if P20ST_C==1|P20ST_C==2
replace trust_parties = 0 if P20ST_C==3|P20ST_C==4

gen trust_parliament = 1 if P20ST_A==1|P20ST_A==2
replace trust_parliament = 0 if P20ST_A==3|P20ST_A==4

gen trust_others = 1 if P55ST==1
replace trust_others = 0 if P55ST==2

gen trust_military = 1 if P20ST_D==1|P20ST_D==2
replace trust_military = 0 if P20ST_D==3|P20ST_D==4

gen trust_religious_organizations = 1 if P20ST_G==1|P20ST_G==2
replace trust_religious_organizations = 0 if P20ST_G==3|P20ST_G==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = S8
replace age = . if S8==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if S7==1
replace sex = 2 if S7==2
replace sex = -99 if S7==.|S7==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S9, 13, 14, 97)
replace religion = 3 if inlist(S9, 9)
replace religion = 4 if inlist(S9, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S9, 11, 12, 96)
replace religion = -99 if inlist(S9, -1, -2)
replace religion = -99 if S9==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC1==2|REEDUC1==3
replace edu = 2 if REEDUC1==4|REEDUC1==5
replace edu = 3 if REEDUC1==6|REEDUC1==7
replace edu = 4 if REEDUC1==1
replace edu = -99 if REEDUC1==-2|REEDUC1==.|REEDUC1==0|REEDUC1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S17, 6, 7)
replace occupation = 2 if inlist(S17, 1, 5, 2)
replace occupation = 6 if inlist(S17, 3)
replace occupation = 9 if inlist(S17, 4)
replace occupation = -99 if inlist(S17, -2, -3, -4, 8)
replace occupation = -99 if S17==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S16A, 1, 2, 3)
replace employment_status = 2 if inlist(S16A, 4, 5, 6, 7)
replace employment_status = -99 if S16A==-2|S16A==-3|S16A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S5==1
replace marital = 2 if S5==2|S5==3
replace marital = -99 if S5==.|S5==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

decode S1NCC, gen(country_birth)
replace country_birth = "Dominican Republic" if country_birth=="Dominican Rep."

gen born_country = 1 if country_birth==country
replace born_country = 0 if country_birth!=country & S1NCC>0 & S1NCC!=.

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino15_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 16

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2011_eng.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 16

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P20ST_A==1|P20ST_A==2
replace trust_govt = 0 if P20ST_A==3|P20ST_A==4

gen trust_justice = 1 if P22ST_B==1|P22ST_B==2
replace trust_justice = 0 if P22ST_B==3|P22ST_B==4

gen trust_police = 1 if P20ST_C==1|P20ST_C==2
replace trust_police = 0 if P20ST_C==3|P20ST_C==4

gen trust_civil_service = 1 if P22ST_E==1|P22ST_E==2
replace trust_civil_service = 0 if P22ST_E==3|P22ST_E==4

gen trust_local_govt = 1 if P22ST_F==1|P22ST_F==2
replace trust_local_govt = 0 if P22ST_F==3|P22ST_F==4

gen trust_parties = 1 if P22ST_F==1|P22ST_F==2
replace trust_parties = 0 if P22ST_F==3|P22ST_F==4

gen trust_parliament = 1 if P22ST_A==1|P22ST_A==2
replace trust_parliament = 0 if P22ST_A==3|P22ST_A==4

gen trust_others = 1 if P25ST==1
replace trust_others = 0 if P25ST==2

gen trust_military = 1 if P22ST_D==1|P22ST_D==2
replace trust_military = 0 if P22ST_D==3|P22ST_D==4

gen trust_religious_organizations = 1 if P22ST_G==1|P22ST_G==2
replace trust_religious_organizations = 0 if P22ST_G==3|P22ST_G==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2011

clonevar age = S17
replace age = . if S17==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if S16==1
replace sex = 2 if S16==2
replace sex = -99 if S16==.|S16==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S18, 13, 14, 97)
replace religion = 3 if inlist(S18, 9)
replace religion = 4 if inlist(S18, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S18, 11, 12, 96)
replace religion = -99 if inlist(S18, -1, -2)
replace religion = -99 if S18==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC1==2|REEDUC1==3
replace edu = 2 if REEDUC1==4|REEDUC1==5
replace edu = 3 if REEDUC1==6|REEDUC1==7
replace edu = 4 if REEDUC1==1
replace edu = -99 if REEDUC1==-2|REEDUC1==.|REEDUC1==0|REEDUC1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S23A, 1, 2, 3)
replace employment_status = 2 if inlist(S23A, 4, 5, 6, 7)
replace employment_status = -99 if S23A==-2|S23A==-3|S23A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S15==1
replace marital = 2 if S15==2|S15==3
replace marital = -99 if S15==.|S15==-2

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino16_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 17

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro2013Eng.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 17

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P26TGB_B==1|P26TGB_B==2
replace trust_govt = 0 if P26TGB_B==3|P26TGB_B==4

gen trust_justice = 1 if P26TGB_E==1|P26TGB_E==2
replace trust_justice = 0 if P26TGB_E==3|P26TGB_E==4

gen trust_police = 1 if P28TGB_B==1|P28TGB_B==2
replace trust_police = 0 if P28TGB_B==3|P28TGB_B==4

gen trust_civil_service = 1 if P26TGB_F==1|P26TGB_F==2
replace trust_civil_service = 0 if P26TGB_F==3|P26TGB_F==4

gen trust_local_govt = 1 if P26TGB_D==1|P26TGB_D==2
replace trust_local_govt = 0 if P26TGB_D==3|P26TGB_D==4

gen trust_parties = 1 if P26TGB_G==1|P26TGB_G==2
replace trust_parties = 0 if P26TGB_G==3|P26TGB_G==4

gen trust_parliament = 1 if P26TGB_C==1|P26TGB_C==2
replace trust_parliament = 0 if P26TGB_C==3|P26TGB_C==4

gen trust_others = 1 if P29STGBS==1
replace trust_others = 0 if P29STGBS==2

gen trust_military = 1 if P28TGB_A==1|P28TGB_A==2
replace trust_military = 0 if P28TGB_A==3|P28TGB_A==4

gen trust_religious_organizations = 1 if P28ST_E==1|P28ST_E==2
replace trust_religious_organizations = 0 if P28ST_E==3|P28ST_E==4

pca trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2013

clonevar age = S11
replace age = . if S11==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if S10==1
replace sex = 2 if S10==2
replace sex = -99 if S10==.|S10==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S14, 13, 14, 97)
replace religion = 2 if inlist(S14, 17)
replace religion = 3 if inlist(S14, 9)
replace religion = 4 if inlist(S14, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15, 18)
replace religion = 5 if inlist(S14, 16)
replace religion = 6 if inlist(S14, 11, 12, 19, 96)
replace religion = -99 if inlist(S14, -1, -2)
replace religion = -99 if S14==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC_1==2|REEDUC_1==3
replace edu = 2 if REEDUC_1==4|REEDUC_1==5
replace edu = 3 if REEDUC_1==6|REEDUC_1==7
replace edu = 4 if REEDUC_1==1
replace edu = -99 if REEDUC_1==-2|REEDUC_1==.|REEDUC_1==0|REEDUC_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S19_A, 1, 2, 3)
replace employment_status = 2 if inlist(S19_A, 4, 5, 6, 7)
replace employment_status = -99 if S19_A==-2|S19_A==-3|S19_A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S9==1
replace marital = 2 if S9==2|S9==3
replace marital = -99 if S9==.|S9==-2|S9==-1

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino17_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 18

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2015_Eng.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 18

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P16ST_G==1|P16ST_G==2
replace trust_govt = 0 if P16ST_G==3|P16ST_G==4

gen trust_justice = 1 if P16ST_H==1|P16ST_H==2
replace trust_justice = 0 if P16ST_H==3|P16ST_H==4

gen trust_police = 1 if P16TGB_B==1|P16TGB_B==2
replace trust_police = 0 if P16TGB_B==3|P16TGB_B==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if P19ST_C==1|P19ST_C==2
replace trust_parties = 0 if P19ST_C==3|P19ST_C==4

gen trust_parliament = 1 if P16ST_F==1|P16ST_F==2
replace trust_parliament = 0 if P16ST_F==3|P16ST_F==4

gen trust_others = 1 if P15STGBS==1
replace trust_others = 0 if P15STGBS==2

gen trust_military = 1 if P16TGB_A==1|P16TGB_A==2
replace trust_military = 0 if P16TGB_A==3|P16TGB_A==4

gen trust_religious_organizations = 1 if P16ST_E==1|P16ST_E==2
replace trust_religious_organizations = 0 if P16ST_E==3|P16ST_E==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2015

clonevar age = S13
replace age = . if S13==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if S12==1
replace sex = 2 if S12==2
replace sex = -99 if S12==.|S12==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S16, 13, 14, 97)
replace religion = 3 if inlist(S16, 9)
replace religion = 4 if inlist(S16, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S16, 11, 12, 96)
replace religion = -99 if inlist(S16, -1, -2)
replace religion = -99 if S16==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC_1==2|REEDUC_1==3
replace edu = 2 if REEDUC_1==4|REEDUC_1==5
replace edu = 3 if REEDUC_1==6|REEDUC_1==7
replace edu = 4 if REEDUC_1==1
replace edu = -99 if REEDUC_1==-2|REEDUC_1==.|REEDUC_1==0|REEDUC_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S22, 6, 7)
replace occupation = 2 if inlist(S22, 1, 5, 2)
replace occupation = 6 if inlist(S22, 3)
replace occupation = 9 if inlist(S22, 4)
replace occupation = -99 if inlist(S22, -2, -3, -4, 8)
replace occupation = -99 if S22==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S21_A, 1, 2, 3)
replace employment_status = 2 if inlist(S21_A, 4, 5, 6, 7)
replace employment_status = -99 if S21_A==-2|S21_A==-3|S21_A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S11==1
replace marital = 2 if S11==2|S11==3
replace marital = -99 if S11==.|S11==-2|S11==-1

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino18_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 19

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro2016Eng_v20170205.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 19

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P13STE==1|P13STE==2
replace trust_govt = 0 if P13STE==3|P13STE==4

gen trust_justice = 1 if P13STF==1|P13STF==2
replace trust_justice = 0 if P13STF==3|P13STF==4

gen trust_police = 1 if P13STGBSB==1|P13STGBSB==2
replace trust_police = 0 if P13STGBSB==3|P13STGBSB==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if P13STG==1|P13STG==2
replace trust_parties = 0 if P13STG==3|P13STG==4

gen trust_parliament = 1 if P13STD==1|P13STD==2
replace trust_parliament = 0 if P13STD==3|P13STD==4

gen trust_others = 1 if P12STGBS==1
replace trust_others = 0 if P12STGBS==2

gen trust_military = 1 if P13STGBSA==1|P13STGBSA==2
replace trust_military = 0 if P13STGBSA==3|P13STGBSA==4

gen trust_religious_organizations = 1 if P13STC==1|P13STC==2
replace trust_religious_organizations = 0 if P13STC==3|P13STC==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = edad
replace age = . if edad==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if sexo==1
replace sex = 2 if sexo==2
replace sex = -99 if sexo==.|sexo==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S8, 13, 14, 97)
replace religion = 3 if inlist(S8, 9)
replace religion = 4 if inlist(S8, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S8, 11, 12, 96)
replace religion = -99 if inlist(S8, -1, -2)
replace religion = -99 if S8==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC_1==2|REEDUC_1==3
replace edu = 2 if REEDUC_1==4|REEDUC_1==5
replace edu = 3 if REEDUC_1==6|REEDUC_1==7
replace edu = 4 if REEDUC_1==1
replace edu = -99 if REEDUC_1==-2|REEDUC_1==.|REEDUC_1==0|REEDUC_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S19, 6, 7)
replace occupation = 2 if inlist(S19, 1, 5, 2)
replace occupation = 6 if inlist(S19, 3)
replace occupation = 9 if inlist(S19, 4)
replace occupation = -99 if inlist(S19, -2, -3, -4, 8)
replace occupation = -99 if S19==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S18A, 1, 2, 3)
replace employment_status = 2 if inlist(S18A, 4, 5, 6, 7)
replace employment_status = -99 if S18A==-2|S18A==-3|S18A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S5==1
replace marital = 2 if S5==2|S5==3
replace marital = -99 if S5==.|S5==-2|S5==-1

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino19_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 20

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro2017Eng_v20180117.dta"

decode idenpa, gen(country)

replace country = "Dominican Republic" if country=="Dominican Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 20

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if P14ST_E==1|P14ST_E==2
replace trust_govt = 0 if P14ST_E==3|P14ST_E==4

gen trust_justice = 1 if P14ST_F==1|P14ST_F==2
replace trust_justice = 0 if P14ST_F==3|P14ST_F==4

gen trust_police = 1 if P14STGBS_B==1|P14STGBS_B==2
replace trust_police = 0 if P14STGBS_B==3|P14STGBS_B==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if P14ST_G==1|P14ST_G==2
replace trust_parties = 0 if P14ST_G==3|P14ST_G==4

gen trust_parliament = 1 if P14ST_D==1|P14ST_D==2
replace trust_parliament = 0 if P14ST_D==3|P14ST_D==4

gen trust_others = 1 if P13STGBS==1
replace trust_others = 0 if P13STGBS==2

gen trust_military = 1 if P14STGBS_A==1|P14STGBS_A==2
replace trust_military = 0 if P14STGBS_A==3|P14STGBS_A==4

gen trust_religious_organizations = 1 if P14ST_C==1|P14ST_C==2
replace trust_religious_organizations = 0 if P14ST_C==3|P14ST_C==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = edad
replace age = . if edad==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if sexo==1
replace sex = 2 if sexo==2
replace sex = -99 if sexo==.|sexo==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S9, 13, 14, 97)
replace religion = 3 if inlist(S9, 9)
replace religion = 4 if inlist(S9, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S9, 11, 12, 96)
replace religion = -99 if inlist(S9, -1, -2)
replace religion = -99 if S9==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC_1==2|REEDUC_1==3
replace edu = 2 if REEDUC_1==4|REEDUC_1==5
replace edu = 3 if REEDUC_1==6|REEDUC_1==7
replace edu = 4 if REEDUC_1==1
replace edu = -99 if REEDUC_1==-2|REEDUC_1==.|REEDUC_1==0|REEDUC_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S19, 6, 7)
replace occupation = 2 if inlist(S19, 1, 5, 2)
replace occupation = 6 if inlist(S19, 3)
replace occupation = 9 if inlist(S19, 4)
replace occupation = -99 if inlist(S19, -2, -3, -4, 8)
replace occupation = -99 if S19==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S18_A, 1, 2, 3)
replace employment_status = 2 if inlist(S18_A, 4, 5, 6, 7)
replace employment_status = -99 if S18_A==-2|S18_A==-3|S18_A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S6==1
replace marital = 2 if S6==2|S6==3
replace marital = -99 if S6==.|S6==-2|S6==-1

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/latino20_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 21

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2018_Eng_Stata_v20190303.dta"

decode IDENPA, gen(country)
replace country = regexs(0) if(regexm(country, "[A-Z][a-z]*"))

replace country = "Dominican Republic" if country=="Rep"
replace country = "El Salvador" if country=="El"
replace country = "Costa Rica" if country=="Costa"
replace country = "Brazil" if country=="Brasil"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 21

gen weight = WT

**********************************
*************TRUST VALUES*********
**********************************

local i 0
foreach varname of varlist * {
    local ++i
    if ( "`varname'" != ustrtoname("`varname'") ) {
        mata : st_varrename(`i', ustrtoname("`varname'") )
    }
}

gen trust_govt = 1 if P15STGBSC_E==1|P15STGBSC_E==2
replace trust_govt = 0 if P15STGBSC_E==3|P15STGBSC_E==4

gen trust_justice = 1 if P15STGBSC_F==1|P15STGBSC_F==2
replace trust_justice = 0 if P15STGBSC_F==3|P15STGBSC_F==4

gen trust_police = 1 if P15STGBSC_B==1|P15STGBSC_B==2
replace trust_police = 0 if P15STGBSC_B==3|P15STGBSC_B==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if P15STGBSC_G==1|P15STGBSC_G==2
replace trust_parties = 0 if P15STGBSC_G==3|P15STGBSC_G==4

gen trust_parliament = 1 if P15STGBSC_D==1|P15STGBSC_D==2
replace trust_parliament = 0 if P15STGBSC_D==3|P15STGBSC_D==4

gen trust_others = 1 if P11STGBS==1
replace trust_others = 0 if P11STGBS==2

gen trust_military = 1 if P15STGBSC_A==1|P15STGBSC_A==2
replace trust_military = 0 if P15STGBSC_A==3|P15STGBSC_A==4

gen trust_religious_organizations = 1 if P15STGBSC_C==1|P15STGBSC_C==2
replace trust_religious_organizations = 0 if P15STGBSC_C==3|P15STGBSC_C==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = NUMINVES

clonevar age = EDAD
replace age = . if EDAD==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if SEXO==1
replace sex = 2 if SEXO==2
replace sex = -99 if SEXO==.|SEXO==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(S5, 13, 14, 97)
replace religion = 3 if inlist(S5, 9)
replace religion = 4 if inlist(S5, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(S5, 11, 12, 96)
replace religion = -99 if inlist(S5, -1, -2)
replace religion = -99 if S5==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if REEDUC_1==1
replace edu = 2 if REEDUC_1==2
replace edu = 3 if REEDUC_1==3
replace edu = -99 if REEDUC_1==-2|REEDUC_1==.|REEDUC_1==0|REEDUC_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(S15, 6, 7)
replace occupation = 2 if inlist(S15, 1, 5, 2)
replace occupation = 6 if inlist(S15, 3)
replace occupation = 9 if inlist(S15, 4)
replace occupation = -99 if inlist(S15, -2, -3, -4, 8)
replace occupation = -99 if S15==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(S14A, 1, 2, 3)
replace employment_status = 2 if inlist(S14A, 4, 5, 6, 7)
replace employment_status = -99 if S14A==-2|S14A==-3|S14A==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if S23==1
replace marital = 2 if S23==2|S23==3
replace marital = -99 if S23==.|S23==-2|S23==-1

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = 1 if S16==1 //this refers to citizenship, but assume this also applies to nationals who were born and raised in country versus naturalised citizens
replace born_country = 0 if S16==2

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight

sort ccode wave survey_year

save "$data_dir/clean/latino21_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 22

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2020_Eng_Stata_v1_0.dta"

decode idenpa, gen(country)
replace country = proper(country)
replace country = strltrim(country)

replace country = "Dominican Republic" if country=="Rep. Dominicana"
replace country = "Mexico" if country=="MéXico"
replace country = "Panama" if country=="Panamá"
replace country = "Peru" if country=="Perú"
replace country = "Brazil" if country=="Brasil"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 22

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p13st_e==1|p13st_e==2
replace trust_govt = 0 if p13st_e==3|p13st_e==4

gen trust_justice = 1 if p13st_f==1|p13st_f==2
replace trust_justice = 0 if p13st_f==3|p13st_f==4

gen trust_police = 1 if P13STGBS_B==1|P13STGBS_B==2
replace trust_police = 0 if P13STGBS_B==3|P13STGBS_B==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if p13st_g==1|p13st_g==2
replace trust_parties = 0 if p13st_g==3|p13st_g==4

gen trust_parliament = 1 if p13st_d==1|p13st_d==2
replace trust_parliament = 0 if p13st_d==3|p13st_d==4

gen trust_others = 1 if p9stgbs==1
replace trust_others = 0 if p9stgbs==2

gen trust_military = 1 if P13STGBS_A==1|P13STGBS_A==2
replace trust_military = 0 if P13STGBS_A==3|P13STGBS_A==4

gen trust_religious_organizations = 1 if p13st_c==1|p13st_c==2
replace trust_religious_organizations = 0 if p13st_c==3|p13st_c==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = numinves

clonevar age = edad
replace age = . if edad==-2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if sexo==1
replace sex = 2 if sexo==2
replace sex = -99 if sexo==.|sexo==-2

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s10, 13, 14, 97)
replace religion = 3 if inlist(s10, 9)
replace religion = 4 if inlist(s10, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s10, 11, 12, 96)
replace religion = -99 if inlist(s10, -1, -2)
replace religion = -99 if s10==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc_1==2|reeduc_1==3
replace edu = 2 if reeduc_1==4|reeduc_1==5
replace edu = 3 if reeduc_1==6|reeduc_1==7
replace edu = 4 if reeduc_1==1
replace edu = -99 if reeduc_1==-2|reeduc_1==.|reeduc_1==0|reeduc_1==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s25, 6, 7)
replace occupation = 2 if inlist(s25, 1, 5, 2)
replace occupation = 6 if inlist(s25, 3)
replace occupation = 9 if inlist(s25, 4)
replace occupation = -99 if inlist(s25, -2, -3, -4, 8)
replace occupation = -99 if s25==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s24_a, 1, 2, 3)
replace employment_status = 2 if inlist(s24_a, 4, 5, 6, 7)
replace employment_status = -99 if s24_a==-2|s24_a==-3|s24_a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight

sort ccode wave survey_year

save "$data_dir/clean/latino22_clean.dta", replace

***************************************************************************************************************

*Latinobarometer 23

clear
use "$data_dir/raw/Latinobarometer/Data/Latinobarometro_2023_Eng_Stata_v1_0.dta"

decode idenpa, gen(country)
replace country = proper(country)
replace country = strltrim(country)

replace country = "Dominican Republic" if country=="Rep. Dominicana"
replace country = "Brazil" if country=="Brasil"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Latinobarometer"
gen wave = 23

gen weight = wt

foreach var of varlist _all {
    local lower = lower("`var'")
    rename `var' `lower'
}

rename s009 S009

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if p13st_e==1|p13st_e==2
replace trust_govt = 0 if p13st_e==3|p13st_e==4

gen trust_justice = 1 if p13st_f==1|p13st_f==2
replace trust_justice = 0 if p13st_f==3|p13st_f==4

gen trust_police = 1 if p13stgbs_b==1|p13stgbs_b==2
replace trust_police = 0 if p13stgbs_b==3|p13stgbs_b==4

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if p13st_g==1|p13st_g==2
replace trust_parties = 0 if p13st_g==3|p13st_g==4

gen trust_parliament = 1 if p13st_d==1|p13st_d==2
replace trust_parliament = 0 if p13st_d==3|p13st_d==4

gen trust_others = 1 if p9stgbs==1
replace trust_others = 0 if p9stgbs==2

gen trust_military = 1 if p13stgbs_a==1|p13stgbs_a==2
replace trust_military = 0 if p13stgbs_a==3|p13stgbs_a==4

gen trust_religious_organizations = 1 if p13st_c==1|p13st_c==2
replace trust_religious_organizations = 0 if p13st_c==3|p13st_c==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2023

clonevar age = edad

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if sexo==1
replace sex = 2 if sexo==2
replace sex = -99 if sexo==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(s1, 13, 14, 97)
replace religion = 3 if inlist(s1, 9)
replace religion = 4 if inlist(s1, 1, 2, 3, 4, 5, 6, 7, 8, 10)
replace religion = 6 if inlist(s1, 11, 12, 96)
replace religion = -99 if inlist(s1, -1, -2)
replace religion = -99 if s1==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if reeduc_3==2|reeduc_3==3
replace edu = 2 if reeduc_3==4|reeduc_3==5
replace edu = 3 if reeduc_3==6|reeduc_3==7
replace edu = 4 if reeduc_3==1
replace edu = -99 if reeduc_3==-2|reeduc_3==.|reeduc_3==0|reeduc_3==-4

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(s19, 6, 7)
replace occupation = 2 if inlist(s19, 1, 5, 2)
replace occupation = 6 if inlist(s19, 3)
replace occupation = 9 if inlist(s19, 4)
replace occupation = -99 if inlist(s19, -3, 8)
replace occupation = -99 if s19==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if inlist(s18_a, 1, 2, 3)
replace employment_status = 2 if inlist(s18_a, 4, 5, 6, 7)
replace employment_status = -99 if s18_a==-2|s18_a==-3|s18_a==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight

sort ccode wave survey_year

save "$data_dir/clean/latino23_clean.dta", replace
